﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlainOldVersionControl.Data;
using System.IO;
using PlainOldVersionControl.BusinessLogic.Base;
using PlainOldVersionControl.Domain;
using PlainOldVersionControl.IViews.Base;
using PlainOldVersionControl.Data.Base;
using PlainOldVersionControl.BusinessLogic;

namespace PlainDBUtil
{
    public partial class MainForm : Form
    {       
        string path = @"C:\eppdata\";

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //dgvCommits.DataSource = CommitDA.GetCommitHistory();
        }

        private void btnInitNewDB_Click(object sender, EventArgs e)
        {            
            BlobBL.InitializeNewDataBase(path);
        }

        private void btnGetDirHash_Click(object sender, EventArgs e)
        {
            string dirHash = TreeBL.GetDirectoryHash(path);
            MessageBox.Show(dirHash);
        }

        private void btnCommitToDataBase_Click(object sender, EventArgs e)
        {
            try
            {
                TreeBL.CreateCommit(new CommitTest());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void btnCompare_Click(object sender, EventArgs e)
        {
            IList<TreeBL.CompareResult> results = TreeBL.CompareTwoTrees("3C5AAE77DC6A8100C9708EE78581D9CD26C2468B", "BD55C9BC7FA6346F9572F1B85E611FEEF5484ABE");

            MessageBox.Show(TreeBL.CompareResult.SimpleToString(results));
        }


        private void btnRetrieve_Click(object sender, EventArgs e)
        {
            BlobBL.RetreiveFromObjectStore("1D00DAC942E76BA336CE10F2A92BC15E87A40EA7", @"C:\FHFile.txt");
        }

        private void btnCompareBlobs_Click(object sender, EventArgs e)
        {
            var diffFiles = BlobBL.CompareFilesFromObjectStore("1D00DAC942E76BA336CE10F2A92BC15E87A40EA7", "6EFA169EC844375133580AD4ABF1987DEF95A125");
            Diff delta = new Diff();
            Diff.Item[] diffs = delta.DiffText(File.ReadAllText(diffFiles.FileAPath), File.ReadAllText(diffFiles.FileBPath));

            foreach (var d in diffs)
            {
                MessageBox.Show(d.deletedA.ToString());
            }            
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            ObjectStoreDA.BackupStore(@"C:\ObjectStoreZip.zip");
        }

        public class CommitTest : ICommit
        {

            public string Hash
            {
                set { ; }
            }

            public string Author
            {
                get
                {
                    return "Aaron";
                }
                set
                {
                    ;
                }
            }

            public string Message
            {
                get
                {
                    return "Hello World Message " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + " " + DateTime.Now.Millisecond;
                }
                set
                {
                    ;
                }
            }

            public DateTime CreatedDate
            {
                set { ; }
            }

            public string TreeHash
            {
                set { ; }
            }

            public string ParentCommit
            {
                set { ; }
            }
        }
    }
}
